/*
 * @Constructor Galleria
 * @功能 图片展示
 * @author Gina
 */
define(function(require,exports,module){
    var Mustache = require("mustache"),
        url = require('mod-url');
    function Galleria(bigImg,smallImg){
        this.$big = $(bigImg);//大图容器
        this.$small = $(smallImg);//缩略图容器
        this.curIndex = 0;//当前索引
        this.preId = 0;//前置Id
        this.nextId = 0;//后置Id
        this.catId = 0;//当前目录
        this.catIndex = 0;//当前目录索引
        this.aCid = 0;//车系Id
        this.aMid = 0;//车款Id
        this.aPid = 0;//图片Id
        this.curPos = 0;//当前图片在类别中的位置
        this.total = 0;//当前目录图片总数
        this.pager = 0;//当前缩略图页数
    }
    module.exports = Galleria;

    /*
     * @name toHtml
     * @功能 转化为html
     * @author Gina
     */
    Galleria.prototype.toHtml = function(data){
        var that = this,
            curData = {cat:data},
            thumb = "{{#cat}}"+
                "<li><img src='{{url}}' width='80' height='55' alt='{{title}}' picsrc='{{picsrc}}' data-index='{{pos}}' /></li>"+
                "{{/cat}}",
            thumbHtml = Mustache.to_html(thumb,curData);

        that.$small.find("ul:first").html(thumbHtml);
    }

    /*
     * @name showIndex
     * @功能 显示当前图片
     * @author Gina
     */
    Galleria.prototype.showIndex = function(index){
        var that = this,
            picSrc,
            picTitle,
            picIndex;

        that.$small.find("li").eq(index).addClass("selected").siblings().removeClass("selected");
        picSrc = that.$small.find("li").eq(index).find("img").attr("picSrc");
        that.$big.find("img").attr("src",picSrc);

        picTitle = that.$small.find("li").eq(index).find("img").attr("alt");
        that.$big.find("h2").text(picTitle);

        picIndex = that.$small.find("li").eq(index).find("img").attr("data-index");
        $("#page").text(picIndex);
    }

    /*
     * @name show
     * @功能 图片展示
     * @author Gina
     */
    Galleria.prototype.show = function(data){
        var that = this,
            curPos = data["params"]["allPos"],//当前图片所属当前目录中的索引值
            catId = data["params"]["catId"],//类别ID
            prev = data["params"]["prev"],//前置ID
            next = data["params"]["next"],//后置ID
            sum = data["params"]["sum"],//当前类别的图片总数
            index = curPos%9,//当前图片索引
            $catList = $("div#gal-cat>ul>li");//类别列表容器

        that.curIndex = index;
        that.preId = prev;
        that.nextId = next;
        that.catId = catId;
        that.curPos = curPos;
        that.total = sum;
        that.pager = curPos/9;

        that.toHtml(data["list"]);//生成缩略图列表结构
        $("#len").text(sum);//图片总数
        //标注当前目录为选中状态
        $catList.each(function(index){
            if($(this).attr("id").substring(7) == catId){
                $(this).addClass("selected").siblings().removeClass("selected");
                that.catIndex = index;
                return;
            }
        });
        that.showIndex(that.curIndex);
    }

    /*
     * @name init
     * @功能 图片展示
     * @author Gina
     */
    Galleria.prototype.init = function(){
        var that = this,
            $pre = $('a.gal-thumb-pre'),
            $next = $('a.gal-thumb-next'),
            $preImg = that.$big.find("a.gal-pre"),
            $nextImg = that.$big.find("a.gal-next"),
            autoTimer = null;

        that.aCid = $("#carInfo").attr('data-carId');
        that.aPid = $("#carInfo").attr('data-pictureId');
        that.aMid = $("#carInfo").attr('data-modelId');
        $.getJSON(url.auto+"?callback=?",{action:"autoPicture", aCid:that.aCid, aPid:that.aPid,aMid:that.aMid },function(data){
            that.show(data);
            //点击图片切换
            that.imgTab();
        });

        //禁用文本选择
        $("div#autoPicture").bind("selectstart",function(){return false;});

        //上一组
        $pre.click(function(){
            that.pager--
            if(that.pager < 0){
                that.preCat();
            }else{
                that.nextPage(that.preId);
            }
        });

        //下一组
        $next.click(function(){
            that.pager++
            if(that.pager > that.total/9){
                that.nextCat();
            }else{
                that.nextPage(that.nextId);
            }

        });

        //上一张图片
        $preImg.click(function(){
            that.preImg();
        });

        //下一张图片
        $nextImg.click(function(){
            that.nextImg();
        });

        //触发键上下盘左右键
        $(document).keyup(function(e) {
            if(e.keyCode == "39"){
                that.nextImg();//左箭头
            }
            if(e.keyCode == '37'){
                that.preImg();//右箭头
            }
        });

        //自动切换图
        $("div#autoPicture").hover(function(){
            clearInterval(autoTimer);
        },function(){
            autoTimer = setInterval(function(){
                that.nextImg();
            },5000);
        }).trigger("mouseout");

        //切换目录
        $("#gal-cat").find("li").each(function(){
            $(this).hover(function(){
                $(this).addClass("over");
            },function(){
                $(this).removeClass("over");
            });
            $(this).click(function(){
                var catId = $(this).attr("id").substring(7);
                $(this).addClass("selected").siblings().removeClass("selected");
                that.catChange(catId);
            });
        });
    }

    /*
     * @name nextPage
     * @功能 得到另一组图片
     * @author Gina
     */
    Galleria.prototype.nextPage = function(imgId){
        var that = this;
        that.$big.find('img').attr("src",lemon.site_url+"Public/Images/auto/loading.gif");
        $.getJSON(url.auto+"?callback=?",{action:"autoPicture", aPid: imgId, aCid:that.aCid, aMid:that.aMid },function(data){
            that.show(data);
            //点击图片切换
            that.imgTab();
        });
    }

    /*
     * @name preImg
     * @功能 上一张图片
     * @author Gina
     */
    Galleria.prototype.preImg = function(){
        var that = this,
            imgIndex = parseInt($("#page").text()),
            catNum = $("div#gal-cat>ul>li").length,
            catId;

        that.curIndex -- ;
        if(that.curIndex < 0 && imgIndex > 1){
            that.nextPage(that.preId);
        }else if(imgIndex <= 1){
            that.preCat();
        }else{
            that.showIndex(that.curIndex);
        }
    }

    /*
     * @name nextImg
     * @功能 下一张图片
     * @author Gina
     */
    Galleria.prototype.nextImg = function(){
        var that = this,
            imgIndex = parseInt($("#page").text());

        that.curIndex ++ ;
        if(that.curIndex >= 9 && imgIndex < that.total){
            that.nextPage(that.nextId);
        }else if(imgIndex >= that.total){
            that.nextCat();
        }else{
            that.showIndex(that.curIndex);
        }
    }

    /*
     * @name catChange
     * @功能 切换类别
     * @author Gina
     */
    Galleria.prototype.catChange = function(catId){
        var that = this;
        that.$big.find('img').attr("src",lemon.site_url+"Public/Images/auto/loading.gif");
        that.catId = catId;
        $.getJSON(url.auto+"?callback=?",{action: "autoPicture", aLid: that.catId, aCid:that.aCid, aMid:that.aMid },function(data){
            that.show(data);
            //点击图片切换
            that.imgTab();
        });
    }

    /*
     * @name nextCat
     * @功能 下一个类别
     * @author Gina
     */
    Galleria.prototype.nextCat = function(){
        var that = this,
            catNum = $("div#gal-cat>ul>li").length,
            catId;
        that.catIndex++;
        if(that.catIndex >= catNum){
            that.catIndex = 0;
        }
        catId =$("div#gal-cat>ul>li").eq(that.catIndex).attr("id").substring(7);
        that.catChange(catId);
    }

    /*
     * @name preCat
     * @功能 上一个类别
     * @author Gina
     */
    Galleria.prototype.preCat = function(){
        var that = this,
            catNum = $("div#gal-cat>ul>li").length,
            catId;
        that.catIndex--;
        if(that.catIndex < 0 ){
            that.catIndex = catNum-1;
        }
        catId = $("div#gal-cat>ul>li").eq(that.catIndex).attr("id").substring(7);
        that.catChange(catId);
    }

    /*
     * @name imgTab
     * @功能 切换图片
     * @author Gina
     */
    Galleria.prototype.imgTab = function(){
        var that = this;
        that.$small.find("li").each(function(index){
            $(this).click(function(){
                that.curIndex = index;
                that.showIndex(that.curIndex);
            });
        });
    }
});